/*
	--------------------------------
	Some recurrently used functions
	by the author.
	--------------------------------
	@author: Luis M. Rodriguez-R
	--------------------------------
*/

// ToDo 
function add_externals(where) {
	var anchors = where.getElementsByTagName('a');
	var internal = /http:\/\/((www\.)?bioinf\.uniandes.edu.co)|javascript:/;
	for ( var i=0; i < anchors.length; ++i ) {
		var href = anchors[i].href;
		if ( !internal.test(href) && href.length>0) {
			anchors[i].innerHTML+= " <img src='"+root_srv+"/img/external.png' alt='ext'/>";
		}
	}
	include(root_srv+"locale/"+bla_lang+"/dict.js");
	return;
}

// DOM handling
function change_title(newTitle) {
	document.title = newTitle;
	return true;
}

// General purposes
function include(file_path) {
	var j = document.createElement("script");
	j.type = "text/javascript";
	j.src = file_path;
	document.body.appendChild(j);
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else var expires = "";
	document.cookie = name+"="+value+expires+"; path="+BME.root;
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

// Some other specific for this package
var MyCheckBoxes=new Array();
function checkBox(rq,id,change){
	if(!MyCheckBoxes[rq]) MyCheckBoxes[rq]=new Array();
	var cb=document.getElementById(id);
	if(cb){
		if(change=="check") cb.checked=true;
		else if(change=="uncheck") cb.checked=false;
		else cb.checked=!cb.checked;
		cb.parentNode.parentNode.className=(cb.checked?'checked':'');
		AJAXer.request("js/check/"+id+"|"+cb.checked,"");
		MyCheckBoxes[rq][id]=cb.checked;
	}
}
function checkCheckBoxes(rq){
	if(MyCheckBoxes[rq]) for(var key in MyCheckBoxes[rq]){
		var cb=document.getElementById(key);
		if(cb && MyCheckBoxes[rq][key]) checkBox(rq,cb.id,'check');
	}
}
function checkAllBoxes(rq){
	var cbs=getCheckBoxes(rq);
	for(var i in cbs) checkBox(rq,cbs[i].id,"check");
}
function uncheckAllBoxes(rq){
	var cbs=getCheckBoxes(rq);
	for(var i in cbs) checkBox(rq,cbs[i].id,"uncheck");
}
function getCheckBoxes(rq){
	var cbs=new Array();
	var totalCbs=0;
	var input=document.getElementsByTagName("input");
	var re = new RegExp("^check_"+rq+"/[0-9\.]+$");
	for(var i in input){
		if(input[i].type=='checkbox' && input[i].id.match(re)){
			cbs[totalCbs++]=input[i];
		}
	}
	return cbs;
}
function execAction(rq,select){
	select.blur();
	if(select[0].selected) return;
	var scream="";
	re=new RegExp("^.*\/([0-9\.]+)$");
	if(MyCheckBoxes[rq])for(var key in MyCheckBoxes[rq])if(MyCheckBoxes[rq][key])scream+=":"+key.replace(re,"$1");
	scream=scream.substr(1);
	for(var i=0; i<select.length; i++) if(select[i].selected) {
		AJAXer.request("js/action/"+select[i].value+"|"+rq+"|"+scream,"");
	}
	select[0].selected="selected";
}
